package com.salon.client.tabItems;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.data.PagingLoadConfig;
import com.extjs.gxt.ui.client.data.RpcProxy;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnData;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridCellRenderer;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.salon.client.GreetingServiceAsync;
import com.salon.client.Salon;
import com.salon.client.beans.ClientBean;
import com.salon.client.beans.MasterBean;
import com.salon.client.beans.MaterialBean;
import com.salon.client.beans.ParentPagingLoadResult;
import com.salon.client.beans.WhereBean;
import com.salon.client.widgets.ClientDialog;
import com.salon.client.widgets.MasterDialog;
import com.salon.client.widgets.MaterialDialog;
import com.salon.client.widgets.MaterialSearchDialog;

public class MaterialTabItemPanel extends ParentTabItemPanel<MaterialBean> {
	private List<WhereBean> wheres = null;
	public MaterialTabItemPanel(String title) {
		super(title);
	}

	@Override
	ColumnModel getColomnModel() {
		List<ColumnConfig> configs = new ArrayList<ColumnConfig>();

		ColumnConfig conf = new ColumnConfig("code", "Код", 50);
		configs.add(conf);
		
		conf = new ColumnConfig("name", "Наименование", 100);
		configs.add(conf);
		
		conf = new ColumnConfig("decs", "Описание", 100);
		configs.add(conf);
		
		conf = new ColumnConfig("made", "Производитель", 100);
		configs.add(conf);
		
		conf = new ColumnConfig("cost", "Стоимость", 50);
		conf.setNumberFormat(NumberFormat.getFormat("#####.## руб"));
		configs.add(conf);
		
		conf = new ColumnConfig("weight", "Вес", 50);
		configs.add(conf);
		
		conf = new ColumnConfig("id", "Ед. исчисл.", 50);
		conf.setRenderer(getIsCountedRenderer());
		configs.add(conf);
		
		return new ColumnModel(configs);
	}
	
	@Override
	protected void initComponents() {
		addButton("Добавить материал", "brit-add-user", new SelectionListener<ButtonEvent>() {
			
			public void componentSelected(ButtonEvent ce) {
				MaterialDialog dlg = new MaterialDialog(MaterialTabItemPanel.this, null);
				dlg.show();
			}
			
		});
		
		addButton("Редактировать материал", "brit-add-user", new SelectionListener<ButtonEvent>() {
			
			public void componentSelected(ButtonEvent ce) {
				MaterialDialog dlg = new MaterialDialog(MaterialTabItemPanel.this, selectedBean);
				dlg.show();
			}
			
		});
		
		addButton("Удалить материал", "brit-delete-user", new SelectionListener<ButtonEvent>() {
			
			public void componentSelected(ButtonEvent ce) {
				if (selectedBean != null) {
					((GreetingServiceAsync) Registry.get(Salon.SERVICE))
					.deleteItem("material", selectedBean.getId(), 
							new AsyncCallback<Void>() {

								@Override
								public void onFailure(Throwable caught) {
									Info.display("Не удалось", "удалить материал");
									
								}

								@Override
								public void onSuccess(Void result) {
									Info.display("Удаление материала", "успешно");
									MaterialTabItemPanel.this.refreshStore();
									
								}
						
					});
				}
			}
		});
		addButton("Поиск", "brit-add-user", new SelectionListener<ButtonEvent>() {
			
			public void componentSelected(ButtonEvent ce) {
				new MaterialSearchDialog(MaterialTabItemPanel.this);
			}
			
		});
		addButton("Показать все", "brit-add-user", new SelectionListener<ButtonEvent>() {
			
			public void componentSelected(ButtonEvent ce) {
				wheres = null;
				refreshStore();
			}
			
		});
		super.initComponents();
	}
	
	public void search(List<WhereBean> list) {
		wheres = list;
		refreshStore();
	}
	@Override
	RpcProxy<ParentPagingLoadResult<MaterialBean>> getProxy() {
		// TODO Auto-generated method stub
		RpcProxy<ParentPagingLoadResult<MaterialBean>> proxy = new RpcProxy<ParentPagingLoadResult<MaterialBean>>() {
			@Override
			protected void load(Object loadConfig,
					AsyncCallback<ParentPagingLoadResult<MaterialBean>> callback) {
				PagingLoadConfig ploadConfig = (PagingLoadConfig) loadConfig;
				int offset = ploadConfig.getOffset();
				((GreetingServiceAsync) Registry.get(Salon.SERVICE))
						.getMaterials(offset, ploadConfig.getLimit(), wheres, 
								callback);

			}
		};
		
		return proxy;
	}
	
	GridCellRenderer<BeanModel> isCountedRenderer = null;

	public GridCellRenderer<BeanModel> getIsCountedRenderer() {
		if (isCountedRenderer == null) {
			isCountedRenderer = new GridCellRenderer<BeanModel>() {

				@Override
				public Object render(BeanModel model, String property,
						ColumnData config, int rowIndex, int colIndex,
						ListStore<BeanModel> store, Grid<BeanModel> grid) {
					MaterialBean materialBean = model.getBean();
					if (materialBean != null) {
						return materialBean.getIsContedAsString();
					}
					return "";
				}
			};
		}
		return isCountedRenderer;
	}

}
